package jp.tuyano.sample.client.registration;

import jp.tuyano.sample.client.form.EmployeeForm;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DatePicker;

public class Register implements EntryPoint {

    private final RegisterServiceAsync registerService =
            GWT.create(RegisterService.class);
        
    private static final String SERVER_ERROR = "SERVER ERROR!!";
    
    private static final String LABEL_TEXT =
        "登録する従業員の情報を入力してください。";
    
    private Label label;
    private TextBox id;
    private TextBox lastName;
    private TextBox firstName;
    private RadioButton[] sex;
    private DatePicker enterDate;
    private Button sendButton;
    
    public void onModuleLoad() {
        
        FlexTable table = new FlexTable();
        id = new TextBox();
        sendButton = new Button("登録");
        id.addBlurHandler(new BlurHandler() {
            
            public void onBlur(BlurEvent event) {
                String inputId = id.getText();
                if (isEmpty(inputId)) {
                    label.setText("IDを入力してください。");
                    return;
                }
                registerService.isAvailable(inputId,
                        new AsyncCallback<Boolean>() {
                    
                    public void onSuccess(Boolean result) {
                        
                        if (result.booleanValue()) {
                            label.setText("IDは使用可能です。");
                            sendButton.setEnabled(true);
                        } else {
                            label.setText("IDは使用不可能です。");
                            sendButton.setEnabled(false);
                        }
                    }
                    
                    public void onFailure(Throwable caught) {
                        
                        label.setText(SERVER_ERROR);
                    }
                });
            }
        });
        lastName = new TextBox();
        firstName = new TextBox();
        label = new Label(LABEL_TEXT);
        Button resetbutton = new Button("RESET");
        resetbutton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                label.setText(LABEL_TEXT);
                id.setText("");
                lastName.setText("");
                firstName.setText("");
            }
        });
        table.setWidget(0, 0, resetbutton);
        table.setText(1, 0, "ID");
        table.setWidget(1, 1, id);
        table.setText(2, 0, "氏");
        table.setWidget(2, 1, lastName);
        table.setText(3, 0, "名");
        table.setWidget(3, 1, firstName);
        sex = new RadioButton[2];
        sex[0] = new RadioButton("sex", "Male");
        sex[1] = new RadioButton("sex", "Female");
        HorizontalPanel sexPanel = new HorizontalPanel();
        sexPanel.add(sex[0]);
        sexPanel.add(sex[1]);
        table.setText(4, 0, "性別");
        table.setWidget(4, 1, sexPanel);
        enterDate = new DatePicker();
        table.setText(5, 0, "入社日");
        table.setWidget(5, 1, enterDate);
        
        sendButton.addClickHandler(new ClickHandler() {
            
            public void onClick(ClickEvent event) {
                
                EmployeeForm form = new EmployeeForm();
                form.setId(id.getText());
                form.setLastName(lastName.getText());
                form.setFirstName(firstName.getText());
                if (sex[0].getValue()) {
                    form.setSex("M");
                } else {
                    form.setSex("F");
                }
                form.setEnterDate(enterDate.getValue());
                registerService.registerEmployee(form,
                        new AsyncCallback<String>() {
                    
                    public void onSuccess(String result) {
                        label.setText(result);
                    }
                    
                    public void onFailure(Throwable caught) {
                        label.setText(SERVER_ERROR);
                    }
                });
                
            }
        });
        table.setWidget(6, 0, sendButton);
        RootPanel.get("register_label").add(label);
        RootPanel.get("register_panel").add(table);
    }
    
    private boolean isEmpty(String str) {
        return (str.length() == 0);
            
     }
}
